(function(factory) {
	if (typeof define === "function" && define.amd) {
		// AMD. Register as an anonymous module.
		define(factory);
	} else {
		// Browser globals
		factory();
	}
}(function() {
	/**
	 * @author 李球 <service@hteos.com>
	 * @class HteOS.page.Page HteOS移动页面组件
	 * @constructor
	 */
	var Page = HteOS.page.Page = function(task) {
		var page = this;
		var $el = $("<div class=\"col-xs-12 hte-page hte-side\">" + 
				"<header class=\"hte-page-header\">" +
					"<h1 class=\"hte-page-name\">" + (task.name || "") + "</h1>" +
					"<div class=\"hte-page-link\">"+
						"<span class=\"metro-icon-arrow-left-3 hte-page-link-btn\"></span>" + (task.prev || "&nbsp;") + 
						//"<span class=\"hte-page-prev\">" + ('设置' || "") + "</span>"+
					"</div>" +
				"</header>" +
				"<div class=\"hte-page-body\">" +
			"</div>" +
		"</div>");
		page.id = "hte-page-" + new Date().getTime();
		$el.attr("id",this.id);
		$el.appendTo($(document.body));
		$el.width($(document.body).width());
		//$el.css("transform","translate3d("+$el.width()+"px,0px,0px)");
		//app.dockId = this.id;
		page.path = task.path;
		page.closable = task.closable;
		page.loadType = task.loadType;
		page.showed = false;
		page.name = task.name || '';
		HteOS.page.PageManager.register(this);
	};
	Page.prototype.getEl = function(){
		return $("#" + this.id);
	};

	/**
	 * 加载远程内容
	 * */
	Page.prototype.load = function() {
		var me = this,body = me.getEl().find(".hte-page-body");
		HteOS.Masker.mask(body,{
			transparent: true,
			msg: '正在加载...'
		});
		if(this.loadType == 'iframe'){
			var iframe = $("<iframe src='"+me.path+"'></iframe>");
			iframe.load(function(){
				HteOS.Masker.unmask(body);
				me.loaded = true;
			});
			iframe.width($(document.body).width());
			iframe.appendTo(body);
		}else{
			$.ajax({
				url: me.path,
				dataType: 'html',
				success: function(html) {
					HteOS.Masker.unmask(body);
					body.html(html);
				},
				error : function(){
					HteOS.Masker.mask(body,{
						modal: false,
						loading:false,
						msg: '加载资源失败：' + me.path
					});
				},
				complete: function() {
					//body.unmask();
					me.loaded = true;
				}
			});
		}
	};

	/**
	 *显示页面
	 */
	Page.prototype.show = function() {
		if(this.showed === true){
			return;
		}
		//TODO 建立事件机制
		var page = this,$el = this.getEl();
		/*// 隐藏其他的侧边栏
		if (HteOS.dock.DockManager.active) {
			HteOS.dock.DockManager.active.hide();
		}*/
		$el.show();
		window.setTimeout(function(){
			$el.css("transform","translate3d(0px, 0px, 0px)");
		},10);
		
		//if (!$.is.advance) {
		
		page.showed = true;
		if (page.path && !page.loaded) {
			page.load();
		}
		HteOS.EventManager.trigger("hte.page.show",this);
	};

	/**
	 * 隐藏页面
	 * */
	Page.prototype.hide = function() {
		if(this.showed === false){
			return ;
		}
		var page = this,$el = this.getEl();
		//if (!$.is.advance) {
		//$el.css('right', 0 - $el.outerWidth());
		var width = $el.outerWidth();
		$el.css("transform","translate3d("+width+"px,0px,0px)");
		page.showed = false;
		window.setTimeout(function(){
				$el.hide();
			},300);
		if(this.closable === true){
			window.setTimeout(function(){
				$el.remove();
				HteOS.page.PageManager.unregister(page);
				HteOS.EventManager.trigger("hte.page.close",page);
			},300);
			
		}
		HteOS.EventManager.trigger("hte.page.hide",this);
	};

	/**
	 * 切换侧边栏
	 * */
	Page.prototype.toggle = function() {
		if (this.showed === true) {
			this.hide();
		} else {
			this.show();
		}
	}
}));